package com.example.backstage.service.Impl;

import com.example.backstage.dao.CategoryDao;
import com.example.backstage.dao.ProductDao;
import com.example.backstage.service.ICategoryService;
import com.example.backstage.service.ProductService;
import com.example.backstage.vo.ProductDetailVo;
import com.example.backstage.vo.ProductListVo;
import com.example.gok_mall.constUtil.ConstUtil;
import com.example.gok_mall.constUtil.DateTimeUtil;
import com.example.gok_mall.constUtil.PropertiesUtil;
import com.example.gok_mall.entity.Category;
import com.example.gok_mall.entity.Product;
import com.example.gok_mall.reponseData.ResponseCode;
import com.example.gok_mall.reponseData.ResultData;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

    @Service
    public class ProductServiceImpl implements ProductService {
        @Autowired
        private ProductDao productDao;

        @Autowired
        private CategoryDao categoryDao;

        @Autowired
        private ICategoryService iCategoryService;

        @Override
        public ResultData UpdateAndInsertProduct(Product product) {
            if (product != null) {
                if (StringUtils.isNotBlank(product.getSubImages())) {
                    String[] subImagesArray = product.getSubImages().split(",");
                    if (subImagesArray.length > 0) {
                        product.setMainImage(subImagesArray[0]);
                    }
                }
                if (product.getId() != null) {
                    //代表是修改商品
                    product.setUpdateTime(new Date());
                    int rowCount = productDao.updateByPrimaryKeySelective(product);
                    if (rowCount > 0) {
                        return ResultData.success("修改商品信息成功");
                    }
                    return ResultData.fail("修改商品信息失败");

                } else {
//                System.out.println("开始插入新增商品");
                    //代表是新增商品
                    product.setCreateTime(new Date());
                    product.setUpdateTime(new Date());
                    int rowCount = productDao.insertSelective(product);
//                System.out.println("执行完新增商品");
                    if (rowCount > 0) {
//                    System.out.println("插入新增商品成功");
                        return ResultData.success("新增商品成功");
                    }
                    return ResultData.fail("新增商品失败");
                }
            }
            return ResultData.fail("新增或者更新产品参数不正确");
        }
        //删除产品
        @Override
        public ResultData deleteProduct(Integer id) {
            int product2 = productDao.deleteByPrimaryKey(id);
            if (id != null) {
                return ResultData.success("删除商品成功");
            } else {
                return ResultData.fail("请输入商品id");
            }
        }
        //更新商品的在售状态
        @Override
        public ResultData<String> setSaleStatus(Integer productId, Integer status) {
            if (productId == null || status == null) {
                return ResultData.fail(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getMage());
            }

            Product product = new Product();
            product.setId(productId);
            product.setStatus(status);

            int rowCount = productDao.updateByPrimaryKeySelective(product);
            if (rowCount > 0) {
                return ResultData.success("修改商品销售状态成功");
            }
            return ResultData.fail("修改商品销售状态失败");
        }
        //List列表查询
//        @Override
//        public List<Product> getProductList(Integer pageNum, Integer pageSize) {
//            //开启分页模式
//            PageHelper.startPage(pageNum,pageSize);
//            List<Product> products = productDao.selectList();
//            PageInfo<Product> pageInfo = new PageInfo<>(products);
//            return products;
//        }
        //List列表查询2
        @Override
        public ResultData<PageInfo> getProductList(int pageNum, int pageSize) {
            //startPage--start
            //填充自己的sql逻辑
            //PageHelper--收尾
            PageHelper.startPage(pageNum,pageSize);
            List<Product> productList=productDao.selectList();
            List<ProductListVo> productListVoList= Lists.newArrayList();
            for (Product productItem: productList){
                ProductListVo productListVo=assembleProductListVo(productItem);
                productListVoList.add(productListVo);
            }

            PageInfo pageResult=new PageInfo(productList);
            pageResult.setList(productListVoList);
            return ResultData.createData(pageResult);
        }

        private ProductListVo assembleProductListVo(Product product){
            ProductListVo productListVo=new ProductListVo();

            productListVo.setId(product.getId());
            productListVo.setSubtitle(product.getSubtitle());
            productListVo.setPrice(product.getPrice());
            productListVo.setMainImage(product.getMainImage());
            productListVo.setCategoryId(product.getCategoryId());
            productListVo.setName(product.getName());
            productListVo.setStatus(product.getStatus());
            productListVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://image.imooc.com/"));

            return productListVo;
        }

        //获取商品的详细信息
        @Override
        public ResultData<ProductDetailVo> manageProductDetail(Integer productId) {
            if (productId == null) {
                return ResultData.fail(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getCode());
            }
            Product product = productDao.selectByPrimaryKey(productId);
            if (product == null) {
                return ResultData.fail("商品已下架或者删除");
            }
            ProductDetailVo productDetailVo = assembleProductDetailVo(product);
            return ResultData.createData(productDetailVo);
        }
            //对返回的商品详细信息进行封装
            private ProductDetailVo assembleProductDetailVo(Product product) {

            ProductDetailVo productDetailVo = new ProductDetailVo();

            productDetailVo.setId(product.getId());
            productDetailVo.setCategoryId(product.getCategoryId());
            productDetailVo.setName(product.getName());
            productDetailVo.setSubtitle(product.getSubtitle());
            //productDetailVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://img.happymmall.com/"));
            productDetailVo.setMainImage(product.getMainImage());
            productDetailVo.setSubImages(product.getSubImages());
            productDetailVo.setDetail(product.getDetail());
            productDetailVo.setPrice(product.getPrice());
            productDetailVo.setStock(product.getStock());
            productDetailVo.setStatus(product.getStatus());

            Category category = categoryDao.selectByPrimaryKey(product.getCategoryId());
            if (category == null) {
                productDetailVo.setParentCategoryId(0);//默认根节点
            } else {
                productDetailVo.setParentCategoryId(category.getParentId());
            }
            productDetailVo.setCreateTime(DateTimeUtil.dateToStr(product.getCreateTime()));
            productDetailVo.setUpdateTime(DateTimeUtil.dateToStr(product.getUpdateTime()));
            return productDetailVo;
        }
        @Override
        //后台商品搜索实现
        public ResultData<PageInfo> searchProduct(String productName, Integer productId, int pageNum, int pageSize) {
                PageHelper.startPage(pageNum,pageSize);
                if(StringUtils.isNotBlank(productName)){
                    productName=new StringBuilder().append("%").append(productName).append("%").toString();
                }
                List<Product> productList=productDao.selectByNameAndProductId(productName,productId);
                List<ProductListVo> productListVoList= Lists.newArrayList();

                for (Product productItem: productList){
                    ProductListVo productListVo=assembleProductListVo(productItem);
                    productListVoList.add(productListVo);
                }
                PageInfo pageResult=new PageInfo(productList);
                pageResult.setList(productListVoList);
                return ResultData.createData(pageResult);
        }
    }
